package campus.rest;

import me.zhengjie.aop.log.Log;
import campus.domain.CampusUserTable;
import campus.service.CampusUserTableService;
import campus.service.dto.CampusUserTableQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author 李诗莹
* @date 2020-03-05
*/
@Api(tags = "用户表管理")
@RestController
@RequestMapping("/api/campusUserTable")
public class CampusUserTableController {

    private final CampusUserTableService campusUserTableService;

    public CampusUserTableController(CampusUserTableService campusUserTableService) {
        this.campusUserTableService = campusUserTableService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('campusUserTable:list')")
    public void download(HttpServletResponse response, CampusUserTableQueryCriteria criteria) throws IOException {
        campusUserTableService.download(campusUserTableService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询用户表")
    @ApiOperation("查询用户表")
    @PreAuthorize("@el.check('campusUserTable:list')")
    public ResponseEntity<Object> getCampusUserTables(CampusUserTableQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(campusUserTableService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增用户表")
    @ApiOperation("新增用户表")
    @PreAuthorize("@el.check('campusUserTable:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CampusUserTable resources){
        return new ResponseEntity<>(campusUserTableService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改用户表")
    @ApiOperation("修改用户表")
    @PreAuthorize("@el.check('campusUserTable:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CampusUserTable resources){
        campusUserTableService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除用户表")
    @ApiOperation("删除用户表")
    @PreAuthorize("@el.check('campusUserTable:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        campusUserTableService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}